try:
    from .type_schema import TypeSchema
    from .prelude import todo
except ImportError:
    from type_schema import TypeSchema
    from prelude import todo

from typing import Any

Json = Any
Data = Json
def qualify(data: 'Data', ts: TypeSchema) -> bool:
    if ts.kind == TypeSchema.Kind.VOID and data is None:
        return True
    elif ts.kind == TypeSchema.Kind.INT and isinstance(data, int):
        return True
    elif ts.kind == TypeSchema.Kind.FLOAT and isinstance(data, (int, float)):
        return True
    elif ts.kind == TypeSchema.Kind.STR and isinstance(data, str):
        return True
    elif ts.kind == TypeSchema.Kind.BOOL and isinstance(data, bool):
        return True
    elif ts.kind == TypeSchema.Kind.JSON: # 任何json对象都符合Json类型
        return True
    elif ts.kind == TypeSchema.Kind.LIST and isinstance(data, list):
        for item in data:
            if not qualify(item, ts.inner):
                return False
        return True
    elif ts.kind == TypeSchema.Kind.MAP and isinstance(data, dict):
        for key, value in data.items():
            if not qualify(key, ts.key) or not qualify(value, ts.value):
                return False
        return True
    elif ts.kind == TypeSchema.Kind.TUPLE and isinstance(data, list):
        for item, t in zip(data, ts.elements):
            if not qualify(item, t):
                return False
        return True
    elif ts.kind == TypeSchema.Kind.OPT:
        if data is None:
            return True
        return qualify(data, ts.inner)
    elif ts.kind == TypeSchema.Kind.DATATYPE:
        return True # 暂时不验证
    else:
        return False
    

